﻿using System.Collections.Generic;
using System.Data;
using Core.Library;
using DataObject.Members;

namespace DataObject.Photo
{
    public class PhotoController
    {
        /***************************************
        = Back End
        -------------------------------------- */
        public static List<PhotoObject> CMS_Photos_GetByMemberID(int id, int round)
        {
            return ObjectHelper.MapRecords<PhotoObject>(PhotoDAL.CMS_Photos_GetByMemberID(id, round));
        }


        /***************************************
        = Front End
        -------------------------------------- */

        internal static DataTable Photos_GetByMemberID(int id, int round)
        {
            DataTable dt = null;
            using (var db = new MainDB())
            {
                dt = db.StoredProcedures.Photos_GetByMemberID(id, round);
            }
            if (dt != null && dt.Rows.Count > 0)
            {
                var user = MembersCache.Members_GetByIdStore(id);

                dt.Columns.Add(Constants.Table_Photos.EncodeTitle);
                dt.Columns.Add(Constants.Table_Photos.IsMaster);
                dt.Columns.Add(Constants.Table_Photos.Width);
                dt.Columns.Add(Constants.Table_Photos.Height);
                for (int i = 0; i < dt.Rows.Count; i++)
                {
                    var r = dt.Rows[i];
                    r[Constants.Table_Photos.EncodeTitle] =
                            UnicodeUtility.EnCodeTitle(r[Constants.Table_Photos.Photo_Title]);
                    if (user != null && user.Member_OriginalAvatar == r[Constants.Table_Photos.Photo_Url].ToString())
                        r[Constants.Table_Photos.IsMaster] = "1";
                    else r[Constants.Table_Photos.IsMaster] = "0";
                    r[Constants.Table_Photos.Width] = Utils.GetSizePhoto(r[Constants.Table_Photos.Photo_Size].ToString());
                    r[Constants.Table_Photos.Height] = Utils.GetSizePhoto(r[Constants.Table_Photos.Photo_Size].ToString(), true);
                }
            }
            return dt;
        }

        public static int Photos_CountByMemberID(int id, int round)
        {
            DataTable dt = null;
            using (var db = new MainDB())
            {
                dt = db.StoredProcedures.Photos_CountByMemberID(id, round);
            }

            if (dt != null && dt.Rows.Count > 0)
                return ConvertUtility.ConvertTo<int>(dt.Rows[0][0]);
            return 0;
        }

        public static int InsertPhotoRound(int Member_ID, string Photo_Title, string Photo_Description, string size, string Photo_Url, int Photo_Round)
        {
            DataTable dt = null;
            int ID = 0;
            using (var db = new MainDB())
            {
                dt = db.StoredProcedures.InsertPhotoRound(Member_ID, Photo_Title, Photo_Description, size, Photo_Url,
                                                          Photo_Round);
            }

            if (dt != null && dt.Rows.Count > 0)
            {
                int.TryParse(dt.Rows[0][0].ToString(), out ID);
            }

            return ID;
        }


        public static DataTable UpdatePhotoRound(int Member_ID, int Photo_ID, string Photo_Description)
        {
            DataTable dt = null;
            using (var db = new MainDB())
            {
                dt = db.StoredProcedures.UpdatePhotoRound(Member_ID, Photo_ID, Photo_Description);
            }



            return dt;
        }

        public static DataTable Photos_DeleteById(int id)
        {
            DataTable dt = null;
            using (var db = new MainDB())
            {
                dt = db.StoredProcedures.Photos_DeleteById(id);
            }

            return dt;
        }

        public static DataTable Photos_DeleteByMemberId(int id, int round)
        {
            DataTable dt = null;
            using (var db = new MainDB())
            {
                dt = db.StoredProcedures.Photos_DeleteByMemberId(id, round);
            }

            return dt;            
        }

        internal static DataTable Photos_ListAllPhotoPaging(int round, int pageIndex, int pageSize)
        {
            DataTable dt = null;
            using (var db = new MainDB())
            {
                dt = db.StoredProcedures.Photos_ListAllPhotoPaging((int)MemberStatus.IsPublished, round, pageIndex, pageSize);
            }
            if (dt != null && dt.Rows.Count > 0)
            {
                dt.Columns.Add(Constants.Table_Members.Url);
                dt.Columns.Add(Constants.Table_Members.EncodeName);
                dt.Columns.Add(Constants.Table_Photos.EncodeTitle);
                dt.Columns.Add(Constants.Table_Photos.ThumbUrl);
                dt.Columns.Add(Constants.Table_Photos.RawUrl);
                dt.Columns.Add(Constants.Table_Members.Member_InRound);
                dt.Columns.Add(Constants.Table_Members.EncodedBase64Title);
                dt.Columns.Add(Constants.Table_Members.Avatar45x45);
                dt.Columns.Add(Constants.Table_Photos.Width);
                dt.Columns.Add(Constants.Table_Photos.Height);
                for (int i = 0; i < dt.Rows.Count; i++)
                {
                    var r = dt.Rows[i];
                    r[Constants.Table_Members.Member_InRound] = round;
                    r[Constants.Table_Members.EncodedBase64Title] = SecurityUtility.Encrypt.EncodeTo64(r[Constants.Table_Members.Member_FullName].ToString());

                    var isOtherAvatar = r[Constants.Table_Members.Member_Avatar].ToString().StartsWith("http");
                    r[Constants.Table_Members.Avatar45x45] = isOtherAvatar ? r[Constants.Table_Members.Member_Avatar].ToString() : StorageAPI.Zoom(45, 45,
                                                                                r[Constants.Table_Members.Member_Avatar].
                                                                                    ToString());
                    r[Constants.Table_Members.Url] =
                            UrlHelpers.MembersDetails(r[Constants.Table_Members.Member_ID], r[Constants.Table_Members.Member_Email], round);
                    r[Constants.Table_Members.EncodeName] =
                            UnicodeUtility.EnCodeTitle(r[Constants.Table_Members.Member_FullName]);
                    r[Constants.Table_Photos.EncodeTitle] =
                            UnicodeUtility.EnCodeTitle(r[Constants.Table_Photos.Photo_Title]);
                    r[Constants.Table_Photos.Photo_Description] = UnicodeUtility.EnCodeTitle(r[Constants.Table_Photos.Photo_Description]);
                    if (r[Constants.Table_Photos.Photo_Url] != null)
                    {
                        r[Constants.Table_Photos.ThumbUrl] = StorageAPI.Thumb_W(300,
                                                                                r[Constants.Table_Photos.Photo_Url].
                                                                                    ToString());
                        r[Constants.Table_Photos.RawUrl] = StorageAPI.Original(r[Constants.Table_Photos.Photo_Url].
                                                                                 ToString());
                        r[Constants.Table_Photos.Width] = Utils.GetSizePhoto(r[Constants.Table_Photos.Photo_Size].ToString());
                        r[Constants.Table_Photos.Height] = Utils.GetSizePhoto(r[Constants.Table_Photos.Photo_Size].ToString(), true);
                    }
                }
            }
            return dt;
        }
    }
}
